package com.xy.annotaion.handler.kotlin.math

import com.xy.annotaion.handler.kotlin.p

const val MAX = 1 shl 30

fun powerOfTwo(cap: Int): Int {
    var n = cap - 1
    p(n.toString(2))
    n = n or (n ushr 1)
    p(n.toString(2))
    n = n or (n ushr 2)
    p(n.toString(2))
    n = n or (n ushr 4)
    p(n.toString(2))
    n = n or (n ushr 8)
    p(n.toString(2))
    n = n or (n ushr 16)
    p(n.toString(2))
    return if (n < 0) 1 else if (n >= MAX) MAX else n + 1
}

fun main() {
    p(powerOfTwo(33));

}
